package com.wolfcode.customer.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

import com.wolfcode.common.core.domain.entity.SysUser;
import com.wolfcode.common.utils.DateUtils;
import com.wolfcode.common.utils.SecurityUtils;
import com.wolfcode.customer.domain.vo.CustomerVo;
import com.wolfcode.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wolfcode.customer.mapper.CustomerMapper;
import com.wolfcode.customer.domain.Customer;
import com.wolfcode.customer.service.ICustomerService;
import org.springframework.util.Assert;

/**
 * 客户信息Service业务层处理
 * 
 * @author wolfcode
 * @date 2022-12-09
 */
@Service
public class CustomerServiceImpl implements ICustomerService 
{
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询客户信息
     * 
     * @param id 客户信息主键
     * @return 客户信息
     */
    @Override
    public Customer selectCustomerById(String id)
    {
        return customerMapper.selectCustomerById(id);
    }

    /**
     * 查询客户信息列表
     * 
     * @param customer 客户信息
     * @return 客户信息
     */
    @Override
    public List<Customer> selectCustomerList(Customer customer)
    {
        return customerMapper.selectCustomerList(customer);
    }

    /**
     * 新增客户信息
     * 
     * @param customerVo 客户信息
     * @return 结果
     */
    @Override
    public int insertCustomer(CustomerVo customerVo)
    {
        //校验参数
        Assert.notNull(customerVo,"非法参数");
        Assert.notNull(customerVo.getCustomerName(),"企业名称不能为空");
        Assert.notNull(customerVo.getLegalLeader(),"法定代表人不能为空");
        Assert.notNull(customerVo.getRegisterDate(),"成立日期不能为空");
        Assert.notNull(customerVo.getOpenState(),"经营状态不能为空");
        Assert.notNull(customerVo.getProvince(),"所属省份不能为空");
        Assert.notNull(customerVo.getRegCapital(),"注册资本不能为空");
        Assert.notNull(customerVo.getIndustry(),"所属行业不能为空");
        Assert.notNull(customerVo,"非法参数");
        Assert.state(customerVo.getCustomerName().length()<=100,"企业名字过长");
        Assert.state(customerVo.getLegalLeader().length()<=30,"法定代表人名字过长");
        Assert.state(customerVo.getRegCapital().compareTo(new BigDecimal(0))>0
                ,"注册资本不合法");
        Assert.state(customerVo.getIndustry().length()<=30,"名字过长");
        Assert.state(customerVo.getRegAddr().length()<=500,"名字过长");
        Assert.state(customerVo.getScope().length()<=500,"名字过长");
        //设置前端未传入参数
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerVo,customer);
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        customer.setInputUser(sysUser.getUserName());
        customer.setInputTime(DateUtils.getNowDate());
        return customerMapper.insertCustomer(customer);
    }

    /**
     * 修改客户信息
     * 
     * @param customerVo 客户信息
     * @return 结果
     */
    @Override
    public int updateCustomer(CustomerVo customerVo)
    {
        //校验参数
        Assert.notNull(customerVo,"非法参数");
        Assert.notNull(customerVo.getCustomerName(),"企业名称不能为空");
        Assert.notNull(customerVo.getLegalLeader(),"法定代表人不能为空");
        Assert.notNull(customerVo.getRegisterDate(),"成立日期不能为空");
        Assert.notNull(customerVo.getOpenState(),"经营状态不能为空");
        Assert.notNull(customerVo.getProvince(),"所属省份不能为空");
        Assert.notNull(customerVo.getRegCapital(),"注册资本不能为空");
        Assert.notNull(customerVo.getIndustry(),"所属行业不能为空");
        Assert.state(customerVo.getCustomerName().length()<=100,"企业名字过长");
        Assert.state(customerVo.getLegalLeader().length()<=30,"法定代表人名字过长");
        Assert.state(customerVo.getRegCapital().compareTo(new BigDecimal(0))>0
                ,"注册资本不合法");
        Assert.state(customerVo.getIndustry().length()<=30,"名字过长");
        Assert.state(customerVo.getRegAddr().length()<=500,"名字过长");
        Assert.state(customerVo.getScope().length()<=500,"名字过长");
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerVo,customer);
        return customerMapper.updateCustomer(customer);
    }

    /**
     * 批量删除客户信息
     * 
     * @param ids 需要删除的客户信息主键
     * @return 结果
     */
    @Override
    public int deleteCustomerByIds(Long[] ids)
    {
        return customerMapper.deleteCustomerByIds(ids);
    }

    /**
     * 删除客户信息信息
     * 
     * @param id 客户信息主键
     * @return 结果
     */
    @Override
    public int deleteCustomerById(String id)
    {
        return customerMapper.deleteCustomerById(id);
    }
}
